// axios
import axios from '@/config/axios/index.ts';
// 模态框和打印组件
import { VxeUI } from '@/config/vxe/index.ts';
import { reactive, ref } from 'vue';
import type { JsonResultProps } from '#/JsonResult.d.ts';
import { FormInst, FormProps, FormRules, FormValidationError } from 'naive-ui';
import type { Options } from '#/Options.d.ts';
import type { DictTypeProps } from '#/src/components/page/systemManager/dictMaintain/DictionaryMaintain.d.ts';
// import { loadDict } from '@/config/dictionary/dictionary.ts';
const useLeftAddModalHooks = (props: any, SlotModal: any) => {
	// ----------------------------------------------------------------------
	const slotModalConfig = reactive({
		reLoad            : () => props.reLoad(),
		onMountedEvent    : null,
		onCloseModalEvent : () => clearFormData(),
		onZoomModalEvent  : () => onZoomModalEvent(),
		resetEvent        : () => clearFormData()
	});
	const formConfig = reactive<FormProps>({
		labelWidth           : 120,
		labelAlign           : 'left',
		labelPlacement       : 'left',
		size                 : 'large',
		requireMarkPlacement : 'right-hanging'
	});
	// ----------------------------------------------------------------------
	const slotModalRef = ref<InstanceType<typeof SlotModal>>();
	const formRef = ref<FormInst>();
	// ----------------------------------------------------------------------
	const formData = reactive<Partial<DictTypeProps>>({});
	const formRules = reactive<FormRules>({
		sort         : {
			required : true,
			type     : 'number',
			trigger  : ['input', 'blur'],
			message  : '排序是必须的'
		},
		dictTypeCode : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典类型编码是必须的'
		},
		dictTypeName : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典类型名称是必须的'
		},
		status       : {
			required : true,
			type     : 'boolean',
			trigger  : ['input', 'blur'],
			message  : '字典类型状态是必选的'
		}
	});
	// ----------------------------------------------------------------------
	// 接口下拉选
	const optionList = reactive<Options>({});
	// ----------------------------------------------------------------------
	const showModal = async () => {
		await slotModalRef.value!.openModal({
			height     : '400',
			width      : '700',
			title      : '新增字典类型',
			showClose  : true,
			showZoom   : true,
			fullscreen : false
		});
		// await initDict();
	};
	const onZoomModalEvent = async () => {};
	const clearFormData = async () => {
		// 清理表单数据
		Object.assign(formData, {
			sort         : null,
			dictTypeCode : null,
			dictTypeName : null,
			status       : true
		});
		// 去除表单校验提示
		formRef.value?.restoreValidation();
	};
	const submitEvent = async () => {
		// 校验菜单
		formRef.value?.validate((errors?: Array<FormValidationError>): void => {
			if (errors) {
				VxeUI.modal.message({ content : '请先完善填写的内容!', status : 'warning', id : 'validateMessage' });
			} else {
				// 提交数据
				axios<any, JsonResultProps<any>, any>({
					method : 'POST',
					url    : '/dict/type/add',
					data   : formData
				}).then((jsonResult: JsonResultProps<any>) => {
					if (jsonResult['status']) {
						// 提示用户
						VxeUI.modal.message({
							content : jsonResult['msg'],
							status  : 'success',
							id      : 'successMessage'
						});
						// 刷新数据
						props.reLoad();
						slotModalRef.value!.closeModal();
					} else {
						VxeUI.modal.message({
							content : jsonResult['msg'],
							status  : 'warning',
							id      : 'failedMessage'
						});
					}
				}).catch((error: unknown) => {
					console.error(error);
					VxeUI.modal.message({
						content : '系统异常,请稍后再试',
						status  : 'error',
						id      : 'errorMessage'
					});
				});
			}
		});
	};
	// -----------------------------------------------------------------------------------
	return {
		slotModalConfig, formConfig,
		slotModalRef, formRef,
		formData, formRules,
		optionList,
		showModal,
		clearFormData, submitEvent
	};
};
export default useLeftAddModalHooks;